<template>
  <div id="app">
    <div class="filter-container">
      <!-- :inline="true"同行排列 -->
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="用户名">
          <el-input placeholder="用户名" v-model="username"></el-input>
        </el-form-item>
        <el-form-item label="身份">
          <el-select placeholder="身份" v-model="admins">
            <el-option label="请选择" value></el-option>
            <el-option label="管理员" value="0"></el-option>
            <el-option label="用户" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="searchs">查询</el-button>
          <el-button type="success" icon="el-icon-plus" @click="addUser">新增</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="header">
      <el-table :data="tableData" border stripe>
        <el-table-column align="center" label="序号" type="index" width="50px" />
        <el-table-column align="center" prop="userCode" label="用户编码" />
        <el-table-column align="center" prop="userName" label="用户姓名" />
        <el-table-column align="center" prop="email" label="用户邮箱" />
        <el-table-column align="center" prop="userUrl" label="用户头像" />
        <el-table-column align="center" prop="admin" label="用户身份" />
        <el-table-column align="center" prop="bytime" label="创建时间" />
        <el-table-column align="center" prop="updatetime" label="修改时间" />
        <el-table-column align="center" label="操作" width="150px">
          <template v-slot="scope">
            <el-tooltip class="item" effect="light" content="用户授权" placement="top-start">
              <el-button
                type="primary"
                size="mini"
                circle
                @click="edit(scope.row)"
                icon="el-icon-edit"
              ></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="light" content="删除用户" placement="top-start">
              <el-button
                type="danger"
                size="mini"
                circle
                @click="del(scope.row)"
                icon="el-icon-delete"
              ></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="light" content="取消授权" placement="top-start">
              <el-button
                type="warning"
                size="mini"
                circle
                @click="refresh(scope.row)"
                icon="el-icon-refresh"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5,10,15,20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="dataSize"
      ></el-pagination>
    </div>
    <addUser ref="addUser" @getData="getData"></addUser>
  </div>
</template>
<script>
import { getUser, setUser, deleteUser, refreshUser } from "../../api/user";
import addUser from "../delog/addUser";
export default {
  name: "user",
  components: {
    addUser
  },
  data() {
    return {
      username: "",
      admins: "",
      index: "",
      value: "",
      tableData: [],
      pageNum: 1,
      pageSize: 5,
      dataSize: 0
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      let obj = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        admin: this.admins,
        userName: this.username
      };
      getUser(obj)
        .then(response => {
          if (response.data.responseCode == 200) {
            this.$message({
              type: "success",
              message: "查询成功!"
            });
            this.tableData = response.data.data.list;
            this.dataSize = response.data.data.total;
          }
        })
        .catch(error => {
          this.$message({
            showClose: true,
            message: "查询失败",
            type: "error"
          });
        });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    },
    searchs() {
      this.getData();
    },
    addUser() {
      this.$refs.addUser.open(true);
    },
    del(val) {
      this.$confirm("是否删除此用户?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let obj = {
            userCode: val.userCode
          };
          deleteUser(obj)
            .then(response => {
              if (response.data.responseCode == 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.getData();
              }
            })
            .catch(error => {
              this.$message({
                type: "info",
                message: "删除失败！"
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    edit(val) {
      this.$confirm("是否将此用户设置成管理员?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let obj = {
            userCode: val.userCode
          };
          setUser(obj)
            .then(response => {
              if (response.data.responseCode == 200) {
                this.$message({
                  type: "success",
                  message: "设置成功!"
                });
                this.getData();
              }
            })
            .catch(error => {
              this.$message({
                type: "info",
                message: "设置失败！"
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "操作取消"
          });
        });
    },
    refresh(val) {
      this.$confirm("是否撤销此用户的管理员权限", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let obj = {
            userCode: val.userCode
          };
          refreshUser(obj)
            .then(response => {
              if (response.data.responseCode == 200) {
                this.$message({
                  type: "success",
                  message: "撤销成功!"
                });
                this.getData();
              }
            })
            .catch(error => {
              this.$message({
                type: "info",
                message: "重置失败！"
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    }
  },
  watch: {
    pageNo: function() {
      this.getData();
    },
    pageSize: function() {
      this.getData();
    }
  }
};
</script>
<style scoped>
.pagination {
  margin-top: 10px;
  text-align: right;
}
.filter-container {
  margin-top: 20px;
  float: right;
  margin-bottom: 20px;
}
.filter-item {
  margin-left: 5px;
}
</style>